#include "customtabstyle.h"

#include <QPainter>
#include <QStyleOptionTab>
#include <QDebug>
CustomTabStyle::CustomTabStyle()
{

}

QSize CustomTabStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const
{
    QSize s = QProxyStyle::sizeFromContents(ct, opt, contentsSize, w);
    if (ct == QStyle::CT_TabBarTab)
    {
        s.transpose();
        s.rwidth() = 125;             // 设置每个tabBar中item的大小
        s.rheight() = 85;
    }
    return s;
}

void CustomTabStyle::drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
     if (element == CE_TabBarTabLabel)
     {
         if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt))
         {
             QRect allRect = tab->rect;
//             qDebug()<<allRect.height();
//             qDebug()<<allRect.width();
             allRect.setWidth(allRect.width() - 5);
             allRect.setHeight(allRect.height() - 2);
             //选中状态
             if (tab->state & QStyle::State_Selected)
             {
                 //save用以保护坐标，restore用来退出状态
                 p->save();
                 p->setBrush(QBrush(0xeed7c1));
                 //矩形
                 p->drawRect(allRect.adjusted(0, 0, 10, 10));
                 p->restore();
             }
             //hover拖拽状态
             else if(tab->state & QStyle::State_MouseOver)
             {
                 p->save();
                 p->setBrush(QBrush(0xeed7c1));
                 //painter->drawRoundedRect(tab->rect, 8, 8);
                 p->drawRect(allRect.adjusted(0, 0, 10, 10));
                 p->restore();
             }
             else
             {
                 p->save();
                 p->setBrush(QBrush(0x78aadc));
                 p->drawRoundedRect(tab->rect, 15, 15);
                 //p->drawRect(allRect.adjusted(0, 0, 10, 10));
                 p->restore();
             }
             QTextOption option;
             option.setAlignment(Qt::AlignCenter);
             p->setFont(QFont("宋体", 12, QFont::Bold));
             //painter->setPen(0xffffff);
             p->drawText(allRect, tab->text, option);
             return;
         }
     }
     if (element == CE_TabBarTab)
     {
         QProxyStyle::drawControl(element, opt, p, w);
     }
}
